package com.thirtydays.frame.util

import android.app.Activity
import android.content.Context
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.DatimePicker
import com.github.gzuliyujiang.wheelpicker.OptionPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode
import com.github.gzuliyujiang.wheelpicker.annotation.DateMode
import com.github.gzuliyujiang.wheelpicker.annotation.TimeMode
import com.github.gzuliyujiang.wheelpicker.contract.OnAddressPickedListener
import com.github.gzuliyujiang.wheelpicker.contract.OnDatimePickedListener
import com.github.gzuliyujiang.wheelpicker.contract.OnOptionPickedListener
import com.github.gzuliyujiang.wheelpicker.entity.DatimeEntity
import com.github.gzuliyujiang.wheelpicker.utility.AddressJsonParser
import com.lxj.xpopup.XPopup
import com.thirtydays.frame.base.bean.GoodsCategoryBean
import com.thirtydays.frame.ui.pop.CenterTipPopView
import com.thirtydays.frame.ui.pop.DealPopupView
import com.thirtydays.frame.ui.pop.PhotoPopView
import com.thirtydays.frame.ui.pop.UploadPopView


/**
 * @author 小垚
 * @创建时间： 2020/12/15
 * @描述： 弹窗工具
 **/
object XpopHelp {
    /**
     * 显示时间
     */
    fun showTime(context: Activity, listenter: OnDatimePickedListener) {
        var picker = DatimePicker(context).apply {
            wheelLayout.setDateLabel("年", "月", "日")
            wheelLayout.setTimeLabel("时", "分", "秒")
            wheelLayout.setDateMode(DateMode.YEAR_MONTH_DAY)
            wheelLayout.setTimeMode(TimeMode.HOUR_12_HAS_SECOND)
            wheelLayout.setRange(DatimeEntity.now(), DatimeEntity.yearOnFuture(100))
        }
        picker.setOnDatimePickedListener(listenter)
        picker.show()
    }

    //城市三级联动
    fun showAddress(context: Activity, listener: OnAddressPickedListener) {
        val picker = AddressPicker(context)
        picker.setAddressMode(
            "city.json", AddressMode.PROVINCE_CITY_COUNTY,
            AddressJsonParser.Builder()
                .provinceCodeField("code")
                .provinceNameField("name")
                .provinceChildField("city")
                .cityCodeField("code")
                .cityNameField("name")
                .cityChildField("area")
                .countyCodeField("code")
                .countyNameField("name")
                .build()
        )
        picker.setOnAddressPickedListener(listener)
        picker.wheelLayout.setOnLinkageSelectedListener { first, second, third ->
            picker.titleView.text = String.format(
                "%s%s%s",
                picker.firstWheelView.formatItem(first),
                picker.secondWheelView.formatItem(second),
                picker.thirdWheelView.formatItem(third)
            )
        }
        picker.show()
    }


    /**
     * 单选弹窗
     */
    fun showOption(context: Activity, index: Int = 0, listener: OnOptionPickedListener) {
        val data = mutableListOf<GoodsCategoryBean>()
        data.add(GoodsCategoryBean(1, "食品生鲜"))
        data.add(GoodsCategoryBean(2, "家用电器"))
        data.add(GoodsCategoryBean(3, "家居生活"))
        data.add(GoodsCategoryBean(4, "医疗保健"))
        data.add(GoodsCategoryBean(5, "酒水饮料"))
        data.add(GoodsCategoryBean(6, "图书音像"))
        val picker = OptionPicker(context)
        picker.setTitle("货物分类")
        picker.setBodyWidth(140)
        picker.wheelView.indicatorColor = -0x10000
        picker.wheelView.textColor = -0xff01
        picker.wheelView.selectedTextColor = -0x10000
        picker.setOnOptionPickedListener(listener)
        picker.wheelLayout.setOnOptionSelectedListener { position, item -> picker.titleView.text = picker.wheelView.formatItem(position) }
        picker.setData(data)
        picker.setDefaultPosition(index)
        picker.show()
    }

    /**
     * 更新弹窗
     */
    fun showUpload(context: Context, isForce: Boolean, desc: String, onUpload: (UploadPopView) -> Unit) {
        XPopup.Builder(context)
            .hasShadowBg(true)
            .dismissOnBackPressed(isForce)
            .dismissOnTouchOutside(isForce)
            .asCustom(UploadPopView(context, isForce, desc, onUpload))
            .show()
    }

    /**
     * 中间弹窗提示
     */
    fun showCenterTip(
        context: Context,
        content: String,
        successColor: Int = -1,
        isBack: Boolean = true,
        onCancel: () -> Unit = {},
        onSuccess: (CenterTipPopView) -> Unit
    ) {
        XPopup.Builder(context)
            .hasShadowBg(true)
            .dismissOnTouchOutside(isBack)
            .dismissOnBackPressed(isBack)
            .asCustom(CenterTipPopView(context, successColor, content, onCancel, onSuccess))
            .show()
    }

    /**
     * 弹窗隐私协议
     */
    fun showDeal(context: Context) {
        XPopup.Builder(context)
            .hasShadowBg(true)
            .asCustom(DealPopupView(context))
            .show()
    }

    /**
     * 弹窗选取相册
     */
    fun showPhoto(context: Context, onClick: (Int) -> Unit) {
        XPopup.Builder(context)
            .hasShadowBg(true)
            .asCustom(PhotoPopView(context, onClick))
            .show()
    }

}